package com.sccba.emtbusiness.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.sccba.common.annotation.DataScope;
import com.sccba.common.utils.SecurityUtils;
import com.sccba.common.utils.StringUtils;
import com.sccba.emtbusiness.domain.YjczEmergencyHandleProcess;
import com.sccba.emtbusiness.domain.YjczEmergencyProcessTemplate;
import com.sccba.emtbusiness.domain.YjczEmergencyScene;
import com.sccba.emtbusiness.service.IYjczEmergencyHandleProcessService;
import com.sccba.emtbusiness.service.IYjczEmergencyProcessTemplateService;
import com.sccba.emtbusiness.service.IYjczEmergencySceneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import com.sccba.emtbusiness.mapper.YjczFbChannelSceneMapper;
import com.sccba.emtbusiness.domain.YjczFbChannelScene;
import com.sccba.emtbusiness.service.IYjczFbChannelSceneService;

/**
 * 应急处置流程Service业务层处理
 * 
 * @author lichh
 * @date 2024-04-18
 */
@Service
public class YjczFbChannelSceneServiceImpl implements IYjczFbChannelSceneService 
{
    @Autowired
    private YjczFbChannelSceneMapper yjczFbChannelSceneMapper;

    @Autowired
    private IYjczEmergencySceneService yjczEmergencySceneService;

    @Autowired
    private IYjczEmergencyHandleProcessService yjczEmergencyHandleProcessService;

    @Autowired
    private IYjczEmergencyProcessTemplateService yjczEmergencyProcessTemplateService;


    /**
     * 查询应急处置流程
     * 
     * @param pkId 应急处置流程主键
     * @return 应急处置流程
     */
    @Override
    public YjczFbChannelScene selectYjczFbChannelSceneByPkId(Long pkId)
    {
        return yjczFbChannelSceneMapper.selectYjczFbChannelSceneByPkId(pkId);
    }

    /**
     * 查询应急处置流程列表
     * 
     * @param yjczFbChannelScene 应急处置流程
     * @return 应急处置流程
     */
    @Override
//    @DataScope(scopeByChannel = true, channelAlias = "c")
    public List<YjczFbChannelScene> selectYjczFbChannelSceneList(YjczFbChannelScene yjczFbChannelScene)
    {
        return yjczFbChannelSceneMapper.selectYjczFbChannelSceneList(yjczFbChannelScene);
    }

    /**
     * 新增应急处置流程
     * 
     * @param yjczFbChannelScene 应急处置流程
     * @return 结果
     */
    @Override
    public int insertYjczFbChannelScene(YjczFbChannelScene yjczFbChannelScene)
    {
        return yjczFbChannelSceneMapper.insertYjczFbChannelScene(yjczFbChannelScene);
    }

    /**
     * 修改应急处置流程
     * 
     * @param yjczFbChannelScene 应急处置流程
     * @return 结果
     */
    @Override
    public int updateYjczFbChannelScene(YjczFbChannelScene yjczFbChannelScene)
    {
        return yjczFbChannelSceneMapper.updateYjczFbChannelScene(yjczFbChannelScene);
    }

    /**
     * 批量删除应急处置流程
     * 
     * @param pkIds 需要删除的应急处置流程主键
     * @return 结果
     */
    @Override
    public int deleteYjczFbChannelSceneByPkIds(Long[] pkIds)
    {
//        return yjczFbChannelSceneMapper.deleteYjczFbChannelSceneByPkIds(pkIds);
        return yjczFbChannelSceneMapper.updateToRemove(pkIds);
    }

    /**
     * 删除应急处置流程信息
     * 
     * @param pkId 应急处置流程主键
     * @return 结果
     */
    @Override
    public int deleteYjczFbChannelSceneByPkId(Long pkId)
    {
        return yjczFbChannelSceneMapper.deleteYjczFbChannelSceneByPkId(pkId);
    }

    @Override
    public void addOrUpdateHandleProcess(YjczFbChannelScene yjczFbChannelScene) {
        if (null == yjczFbChannelScene) {
            return;
        }
        Long pkId = yjczFbChannelScene.getPkId();
        Long sceneId = yjczFbChannelScene.getSceneId();
        YjczEmergencyScene scene = yjczFbChannelScene.getScene();
        List<YjczEmergencyHandleProcess> processList = yjczFbChannelScene.getHandleProcessList();
        String currUser = String.valueOf(SecurityUtils.getUserId());
        Date currDate = Calendar.getInstance().getTime();
        List<YjczEmergencyProcessTemplate> templateList = new ArrayList<>();
        YjczEmergencyProcessTemplate template;
        Integer updateTemplateFlag = yjczFbChannelScene.getUpdateTemplateFlag();
        if (null != pkId && pkId != 0) {
            // 修改
            // 1.修改场景备注
            if (null != scene) {
                yjczEmergencySceneService.updateYjczEmergencyScene(scene);
            }
            List<YjczEmergencyHandleProcess> needInsert = new ArrayList<>();
            List<YjczEmergencyHandleProcess> needUpdate = new ArrayList<>();
            List<Long> needDelete = new ArrayList<>();
            // 2.更新处置流程
            for (YjczEmergencyHandleProcess process : processList) {
                Long processPkId = process.getPkId();
                if ("2".equals(process.getDelFlag())) {
                    needDelete.add(processPkId);
                    continue;
                }
                if (null == processPkId || processPkId == 0) {
                    process.setCreateBy(currUser);
                    process.setCreateTime(currDate);
                    process.setNodeFbChannelSceneId(pkId);
                    needInsert.add(process);
                } else {
                    process.setUpdateBy(currUser);
                    process.setUpdateTime(currDate);
                    needUpdate.add(process);
                }
                template = YjczEmergencyProcessTemplate.getTemplateByProcess(process);
                template.setNodeSceneId(sceneId);
                templateList.add(template);
            }
            if (!needInsert.isEmpty()) {
                yjczEmergencyHandleProcessService.batchInsert(needInsert);
            }
            if (!needUpdate.isEmpty()) {
                yjczEmergencyHandleProcessService.batchUpdate(needUpdate);
            }
            if (!needDelete.isEmpty()) {
                yjczEmergencyHandleProcessService.batchDelete(needDelete);
            }
            // 3.更新场景关联的流程，先删后增
            if (1 == updateTemplateFlag) {
                yjczEmergencyProcessTemplateService.deleteTemplateBySceneId(sceneId);
                yjczEmergencyProcessTemplateService.batchInsert(templateList);
            }
        } else {
            // 新增
            // 1.判断是否新增应急场景，需要先新增，不需要则先删除模板数据
            if (null == sceneId && (null == scene || StringUtils.isEmpty(scene.getSceneName()))) {
                return;
            }
            if (null == sceneId) {
                yjczEmergencySceneService.insertYjczEmergencyScene(scene);
                sceneId = scene.getPkId();
                yjczFbChannelScene.setSceneId(sceneId);
            } else if (1 == updateTemplateFlag) {
                yjczEmergencyProcessTemplateService.deleteTemplateBySceneId(sceneId);
            }
            // 2.新增关联关系
            yjczFbChannelSceneMapper.insertYjczFbChannelScene(yjczFbChannelScene);
            // 3.新增应急流程模板以及应急操作流程
            for (YjczEmergencyHandleProcess process : processList) {
                process.setCreateBy(currUser);
                process.setCreateTime(currDate);
                process.setNodeFbChannelSceneId(yjczFbChannelScene.getPkId());
                template = YjczEmergencyProcessTemplate.getTemplateByProcess(process);
                template.setNodeSceneId(sceneId);
                templateList.add(template);
            }
            if (1 == updateTemplateFlag) {
                yjczEmergencyProcessTemplateService.batchInsert(templateList);
            }
            yjczEmergencyHandleProcessService.batchInsert(processList);
        }
    }
}
